package xyz.xiezc.www.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wb-xzc291800
 * @date 2019/04/24 9:25
 */
public class XUtil {
    public static List<Integer> createPage(IPage mzituBeanPage) {
        int[] rainbow = rainbow((int) mzituBeanPage.getCurrent(), (int) mzituBeanPage.getPages(), 7);
        List<Integer> list = new ArrayList<>();
        if (rainbow[0] == 2) {
            list.add(1);
        }
        if (rainbow[0] > 2) {
            list.add(1);
            list.add(-1);
        }
        for (int r : rainbow) {
            list.add(r);
        }
        if (rainbow[rainbow.length - 1] < mzituBeanPage.getPages() - 1) {
            list.add(-1);
            list.add((int) mzituBeanPage.getPages());
        }
        if (rainbow[rainbow.length - 1] == mzituBeanPage.getPages() - 1) {
            list.add((int) mzituBeanPage.getPages());
        }
        return list;
    }

    public static int[] rainbow(int currentPage, int pageCount, int displayCount) {
        boolean isEven = true;
        isEven = displayCount % 2 == 0;
        int left = displayCount / 2;
        int right = displayCount / 2;
        int length = displayCount;
        if (isEven) {
            ++right;
        }

        if (pageCount < displayCount) {
            length = pageCount;
        }

        int[] result = new int[length];
        int i;
        if (pageCount >= displayCount) {
            if (currentPage <= left) {
                for (i = 0; i < result.length; ++i) {
                    result[i] = i + 1;
                }
            } else if (currentPage > pageCount - right) {
                for (i = 0; i < result.length; ++i) {
                    result[i] = i + pageCount - displayCount + 1;
                }
            } else {
                for (i = 0; i < result.length; ++i) {
                    result[i] = i + currentPage - left + (isEven ? 1 : 0);
                }
            }
        } else {
            for (i = 0; i < result.length; ++i) {
                result[i] = i + 1;
            }
        }

        return result;
    }



}
